package 回溯;

import java.util.ArrayList;
import java.util.List;

/**
 * @author TTDB
 * @create 2025/9/18
 */
public class combinationSum {
    // 39. 组合总和-无限制选取
    public List<List<Integer>> combinationSum(int[] candidates, int target)
    {
        List<List<Integer>> res = new ArrayList<>();
        List<Integer> path = new ArrayList<>();
        dfs(res, path, candidates, target, 0);
        return res;
    }
    public void dfs(List<List<Integer>> res, List<Integer> path, int[] candidates, int target, int start)
    {
        if(target < 0) return;
        if(target == 0) {
            res.add(new ArrayList<>(path));
            return;
        }
        for(int i = start; i < candidates.length; i++) {
            path.add(candidates[i]);
            dfs(res, path, candidates, target - candidates[i], i);//这里不对i处理
            path.remove(path.size() - 1);
        }
    }
}
